<template>
  <div id="login">
    <div class="modal-mask">
      <div class="modal-wrapper">
        <div class="modal-container">
          <div class="main">
            <h1>Ever Note</h1>
          </div>
          <div class="form">
            <div class="toggle-wrapper">
              <div class="toggle">
                <span :class="isShowRegister && 'checked'" @click="showRegister()"
                  >Sign Up</span
                >
                <span :class="isShowLogin && 'checked'" @click="showLogin()">Sign In</span>
              </div>
            </div>

             <div class="switch-wrapper">
              <div class="switch">
                <span :class="isShowRegister && 'checked'" @click="showRegister()"
                  >Sign Up</span
                >
                <span>Or</span>
                <span :class="isShowLogin && 'checked'" @click="showLogin()">Sign In</span>
              </div>
            </div>


            <div v-show="isShowRegister" class="register" transition="expand">
              <h4>用户名</h4>
              <input
                type="text"
                placeholder="用户名"
                v-model="register.username"
              />
              <h4>密码</h4>
              <input
                type="password"
                placeholder="密码"
                v-model="register.password"
              />
              <p v-bind:class="{ error: register.isError }">
                {{ register.notice }}
              </p>
              <div class="button" @click="onRegister()">注册</div>
            </div>

            <div v-show="isShowLogin" class="login" transition="expand">
              <h4>用户名</h4>
              <input
                type="text"
                placeholder="输入用户名"
                v-model="login.username"
              />
              <h4>密码</h4>
              <input
                type="password"
                placeholder="密码"
                v-model="login.password"
              />
              <p v-bind:class="{ error: login.isError }">{{ login.notice }}</p>
              <div class="button" @click="onLogin()">登录</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters, mapActions, mapMutations } from "vuex";

export default {
  data() {
    return {
      isShowRegister: false,
      isShowLogin: true,
      login: {
        username: "",
        password: "",
        notice: "输入用户名和密码",
        isError: false
      },
      register: {
        username: "",
        password: "",
        notice: "创建账号后，请记住用户名和密码",
        isError: false
      }
    };
  },

  methods: {
    ...mapActions({
      userLogin: "login",
      userRegister: "register"
    }),
    showRegister() {
      this.isShowRegister = true;
      this.isShowLogin = false;
    },
    showLogin() {
      this.isShowRegister = false;
      this.isShowLogin = true;
    },
    onRegister() {
      if (!/^[\w\u4e00-\u9fa5]{3,15}$/.test(this.register.username)) {
        this.register.isError = true;
        this.register.notice = "用户名3~15个字符，仅限于字母数字下划线中文";
        return;
      }
      if (!/^.{6,16}$/.test(this.register.password)) {
        this.register.isError = true;
        this.register.notice = "密码长度为6~16个字符";
        return;
      }

      this.userRegister({
        username: this.register.username,
        password: this.register.password
      })
        .then(() => {
          this.register.isError = false;
          this.register.notice = "";
          this.$router.push("/notebooks");
        })
        .catch(data => {
          this.register.isError = true;
          this.register.notice = data.msg;
        });
    },
    onLogin() {
      if (!/^[\w\u4e00-\u9fa5]{3,15}$/.test(this.login.username)) {
        this.login.isError = true;
        this.login.notice = "用户名3~15个字符，仅限于字母数字下划线中文";
        return;
      }
      if (!/^.{6,16}$/.test(this.login.password)) {
        this.login.isError = true;
        this.login.notice = "密码长度为6~16个字符";
        return;
      }

      this.userLogin({
        username: this.login.username,
        password: this.login.password
      })
        .then(() => {
          this.$router.push("/notebooks");
          this.login.isError = false;
          this.login.notice = "";
        })
        .catch(() => {
          this.login.isError = true;
        });
    }
  }
};
</script>
<style lang="less" scoped>

.modal-mask {
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  display: table;
  transition: opacity 0.3s ease;
}

.modal-wrapper {
  display: table-cell;
  vertical-align: middle;
}

.modal-container {
  width: 1000px;
  height: 600px;
  margin: 0px auto;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
  transition: all 0.3s ease;
  font-family: Helvetica, Arial, sans-serif;
  display: flex;

  .main {
     width: 500px;
  
     background:url('https://typrobed-1308357281.cos.ap-chongqing.myqcloud.com/imagebed/202112131117052.png') center, linear-gradient(180deg, rgba(110,226,203,1) 0%, rgba(80,193,183,1) 100%);
    background-repeat: no-repeat;
    >h1{
      margin-top: 100px;
      text-align: center;
      font-weight: 500;
      color: #f3fffe;

    }
  
  }
  .form {
    background-color: #364960;
    width: 500px;
    border-left: 1px solid #ccc;
    .toggle-wrapper {
      display: flex;
      justify-content: flex-end;
      flex-direction: row;
      .toggle {
        margin: 20px 30px;
        color: #f3fffe;
        display: flex;
        flex-direction: row;
        background-color: #48576b;
        align-items: center;
        height: 34px;
        border-radius: 18px;
        overflow: hidden;
    
        > span {
          display: block;
          display: flex;
          align-items: center;
          font-size: 14px;
          height: 100%;
          cursor: pointer;
          padding: 0 15px;
          &.checked{
            background-color: #59ccbe;
          }
     
        }
      }
    }
     

      .switch-wrapper {
      margin-top: 40px;
      display: flex;
      justify-content: flex-start;
      flex-direction: row;
      .switch {
        margin: 10px 30px;
        color: #f3fffe;
        display: flex;
        flex-direction: row;
        align-items: center;
        height: 34px;
        border-radius: 18px;
    
        > span {
          display: block;
          display: flex;
          align-items: center;
          font-size: 18px;
          height: 100%;
          cursor: pointer;
          padding: 0 15px;
          &.checked{
            border-bottom: 2px solid #59ccbe ;
          }
     
        }
      }
    }

   
   h3 {
      padding: 10px 20px;
      font-weight: normal;
      font-size: 16px;
      border-top: 1px solid #eee;
      cursor: pointer;
      &:nth-of-type(2) {
        border-bottom: 1px solid #eee;
      }
    }

    .button {
      background-color: #58c9bc;
      width: 100px;
      border-radius: 18px;
      height: 36px;
      line-height: 36px;
      text-align: center;
      font-weight: bold;
      color: #f3fffe;
      margin-top: 18px;
      cursor: pointer;
    }

    .login,
    .register {
      >h4{
        margin-top: 10px;
        font-weight: 400;
        font-size: 14px;
        color: #cedae9;
      }
      padding: 0px 20px;
      
      .expand-transition {
        transition: all 2s ease;
        overflow: hidden;
        height: 192px;
      }
      .expand-enter,
      .expand-leave {
        height: 0;
      }
      input {
        display: block;
        width: 80%;
        height: 35px;
        line-height: 35px;
        padding: 0 6px;
        border-radius: 4px;
        color: #5f6c7f;
        outline: none;
        border: none;
        border-bottom:1px solid #5f6c7f;
        outline: none;
        font-size: 14px;

        background-color: #364960;
      }


      p {
        font-size: 12px;
        margin-top: 10px;
        color: #444;
      }
      .error {
        color:white
         }
    }
    .login {
      border-top: -1px;
    }
  }
}
</style>
